/*
 * Copyright 2013 Agustina Martinez-Garcia

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */
package uk.ljmu.fedoraifz.wadl.fedora34;

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.HashMap;
import java.util.List;

import javax.activation.DataSource;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;

import org.apache.commons.mail.ByteArrayDataSource;
import org.apache.log4j.Logger;
import org.igfay.jfig.JFig;
import org.igfay.jfig.JFigException;
import org.jvnet.ws.wadl.util.DSDispatcher;
import org.jvnet.ws.wadl.util.JAXBDispatcher;
import org.jvnet.ws.wadl.util.UriBuilder;

import uk.ljmu.fedoraifz.restrequests.UserCredentials;

public class PidDatastreams {

    //private JAXBDispatcher _jaxbDispatcher;
    private DSDispatcher _dsDispatcher;
    private UriBuilder _uriBuilder;
    @SuppressWarnings("unused")
    private JAXBContext _jc;
    private HashMap<String, Object> _templateAndMatrixParameterValues;
    private List<String> matrixParamSet;
    private DataSource retVal;
    private static Logger log = Logger.getLogger(PidDatastreams.class);

    public void setMatrixParamSet(List<String> matrixParamSet) {
	this.matrixParamSet = matrixParamSet;
    }

    public List<String> getMatrixParamSet() {
	return matrixParamSet;
    }

    public void setRetVal(DataSource retVal) {
	this.retVal = retVal;
    }

    public DataSource getRetVal() {
	return retVal;
    }

    /**
     * Create new instance
     * 
     */
    public PidDatastreams(String pid)
    throws JAXBException
    {
	_dsDispatcher = new DSDispatcher();
	_uriBuilder = new UriBuilder();
	String basePath;
	try {
	    basePath = JFig.getInstance().getValue("fedoraServer", "restService");
	} catch (JFigException e) {
	    log.error("Error: ", e);
	    //Error, use default Fedora path
	    basePath = "http://localhost:8080/fedora";
	}
	matrixParamSet = _uriBuilder.addPathSegment(basePath);
	matrixParamSet = _uriBuilder.addPathSegment("/objects");
	matrixParamSet = _uriBuilder.addPathSegment("/{pid}/datastreams");
	_templateAndMatrixParameterValues = new HashMap<String, Object>();
	_templateAndMatrixParameterValues.put("pid", pid);
    }

    /**
     * Get pid
     * 
     */
    public String getPid() {
	return ((String) _templateAndMatrixParameterValues.get("pid"));
    }

    /**
     * Set pid
     * 
     */
    public void setPid(String pid) {
	_templateAndMatrixParameterValues.put("pid", pid);
    }

    public void get()
    throws IOException, MalformedURLException
    {
	HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	retVal = _dsDispatcher.doGET(_url, _headerParameterValues, null);
	return ;
    }

    public void get(UserCredentials userCreds)
    throws IOException, MalformedURLException
    {
	HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	//AMG - Add credentials
	_headerParameterValues.put("Authorization", userCreds.getEncodedCredentials());
	String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	retVal = _dsDispatcher.doGET(_url, _headerParameterValues, null);
	return ;
    }

    /*public void getAsvoid()
    throws IOException, MalformedURLException, JAXBException
    {
	HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	Object _retVal = _jaxbDispatcher.doGET(_url, _headerParameterValues, null);
	if (_retVal == null) {
	    return ;
	}
	if (JAXBElement.class.isInstance(_retVal)) {
	    JAXBElement<?> jaxbElement = ((JAXBElement<?>) _retVal);
	    _retVal = jaxbElement.getValue();
	}
	return ;
    }*/

    public void get(String asofdatetime, String format)
    throws IOException, MalformedURLException
    {
	HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	if (asofdatetime != null && !asofdatetime.equals(""))
	    _queryParameterValues.put("asOfDateTime", asofdatetime);
	if (format != null && !format.equals(""))
	    _queryParameterValues.put("format", format);
	String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	retVal = _dsDispatcher.doGET(_url, _headerParameterValues, null);
	return ;
    }

    public void get(UserCredentials userCreds, String asofdatetime, String format)
    throws IOException, MalformedURLException
    {
	HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	//AMG - Add credentials
	_headerParameterValues.put("Authorization", userCreds.getEncodedCredentials());
	if (asofdatetime != null && !asofdatetime.equals(""))
	    _queryParameterValues.put("asOfDateTime", asofdatetime);
	if (format != null && !format.equals(""))
	    _queryParameterValues.put("format", format);
	String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	retVal = _dsDispatcher.doGET(_url, _headerParameterValues, null);
	return ;
    }

    /*public void getAsvoid(String asofdatetime, String format)
    throws IOException, MalformedURLException, JAXBException
    {
	HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	_queryParameterValues.put("asOfDateTime", asofdatetime);
	_queryParameterValues.put("format", format);
	String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	Object _retVal = _jaxbDispatcher.doGET(_url, _headerParameterValues, null);
	if (_retVal == null) {
	    return ;
	}
	if (JAXBElement.class.isInstance(_retVal)) {
	    JAXBElement<?> jaxbElement = ((JAXBElement<?>) _retVal);
	    _retVal = jaxbElement.getValue();
	}
	return ;
    }*/

    public static class DsID {
	private static final String DEFAULT_CONTENT_TYPE = "application/octet-stream";
	private JAXBDispatcher _jaxbDispatcher;
	private DSDispatcher _dsDispatcher;
	private UriBuilder _uriBuilder;
	@SuppressWarnings("unused")
	private JAXBContext _jc;
	private HashMap<String, Object> _templateAndMatrixParameterValues;
	private List<String> matrixParamSet;
	private DataSource retVal;

	public void setMatrixParamSet(List<String> matrixParamSet) {
	    this.matrixParamSet = matrixParamSet;
	}

	public List<String> getMatrixParamSet() {
	    return matrixParamSet;
	}

	public void setRetVal(DataSource retVal) {
	    this.retVal = retVal;
	}

	public DataSource getRetVal() {
	    return retVal;
	}

	/**
	 * Create new instance
	 * 
	 */
	public DsID(String pid, String dsid)
	throws JAXBException
	{
	    _dsDispatcher = new DSDispatcher();
	    _uriBuilder = new UriBuilder();
	    String basePath;
	    try {
		basePath = JFig.getInstance().getValue("fedoraServer", "restService");
	    } catch (JFigException e) {
		log.error("Error: ", e);
		//Error, use default Fedora path
		basePath = "http://localhost:8080/fedora";
	    }
	    matrixParamSet = _uriBuilder.addPathSegment(basePath);
	    matrixParamSet = _uriBuilder.addPathSegment("/objects");
	    matrixParamSet = _uriBuilder.addPathSegment("/{pid}/datastreams");
	    matrixParamSet = _uriBuilder.addPathSegment("/{dsID}");
	    _templateAndMatrixParameterValues = new HashMap<String, Object>();
	    _templateAndMatrixParameterValues.put("pid", pid);
	    _templateAndMatrixParameterValues.put("dsID", dsid);
	}
	public DsID(String pid, String dsid, boolean getContent)
	throws JAXBException {
	    String basePath;
	    try {
		basePath = JFig.getInstance().getValue("fedoraServer", "restService");
	    } catch (JFigException e) {
		log.error("Error: ", e);
		//Error, use default Fedora path
		basePath = "http://localhost:8080/fedora";
	    }
	    _dsDispatcher = new DSDispatcher();
	    _uriBuilder = new UriBuilder();
	    setMatrixParamSet(_uriBuilder.addPathSegment(basePath));
	    setMatrixParamSet(_uriBuilder.addPathSegment("/objects"));
	    setMatrixParamSet(_uriBuilder.addPathSegment("/{pid}/datastreams"));
	    setMatrixParamSet(_uriBuilder.addPathSegment("/{dsID}"));
	    if (getContent)
		setMatrixParamSet(_uriBuilder.addPathSegment("/{content}"));
	    _templateAndMatrixParameterValues = new HashMap<String, Object>();
	    _templateAndMatrixParameterValues.put("pid", pid);
	    _templateAndMatrixParameterValues.put("dsID", dsid);
	    if (getContent)
		_templateAndMatrixParameterValues.put("content", "content");
	}
	/**
	 * Get pid
	 * 
	 */
	public String getPid() {
	    return ((String) _templateAndMatrixParameterValues.get("pid"));
	}

	/**
	 * Set pid
	 * 
	 */
	public void setPid(String pid) {
	    _templateAndMatrixParameterValues.put("pid", pid);
	}

	/**
	 * Get dsID
	 * 
	 */
	public String getDsid() {
	    return ((String) _templateAndMatrixParameterValues.get("dsID"));
	}

	/**
	 * Set dsID
	 * 
	 */
	public void setDsid(String dsid) {
	    _templateAndMatrixParameterValues.put("dsID", dsid);
	}

	public void post(UserCredentials userCreds, String contents, String mimetype)
	throws IOException, MalformedURLException
	{
	    HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	    HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	    //AMG - Add credentials
	    _headerParameterValues.put("Authorization", userCreds.getEncodedCredentials());
	    String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	    setRetVal(_dsDispatcher.doPOST(new ByteArrayDataSource(contents, mimetype), DEFAULT_CONTENT_TYPE, _url, _headerParameterValues, null));
	    return ;
	}
	/*public void postAsvoid()
	throws IOException, MalformedURLException, JAXBException
	{
	    HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	    HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	    String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	    Object _retVal = _jaxbDispatcher.doPOST(null, null, _url, _headerParameterValues, null);
	    if (_retVal == null) {
		return ;
	    }
	    if (JAXBElement.class.isInstance(_retVal)) {
		JAXBElement<?> jaxbElement = ((JAXBElement<?>) _retVal);
		_retVal = jaxbElement.getValue();
	    }
	    return ;
	}
	 */
	public void post(String controlgroup, String dslocation, String altids, String dslabel, Boolean versionable, String dsstate, String formaturi, String checksumtype, String checksum, String mimetype, String logmessage)
	throws IOException, MalformedURLException
	{
	    HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	    HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	    //AMG MODS to handle either url of string contents
	    if (dslocation.startsWith("http")) {
		_queryParameterValues.put("dsLocation", dslocation);
	    }
	    if (controlgroup != null && !controlgroup.equals(""))
		_queryParameterValues.put("controlGroup", controlgroup);
	    if (altids != null && !altids.equals(""))
		_queryParameterValues.put("altIDs", altids);
	    if (dslabel != null && !dslabel.equals(""))
		_queryParameterValues.put("dsLabel", dslabel);
	    if (versionable != null && !versionable.equals(""))
		_queryParameterValues.put("versionable", versionable);
	    if (dsstate != null && !dsstate.equals(""))
		_queryParameterValues.put("dsState", dsstate);
	    if (formaturi != null && !formaturi.equals(""))
		_queryParameterValues.put("formatURI", formaturi);
	    if (checksumtype != null && !checksumtype.equals(""))
		_queryParameterValues.put("checksumType", checksumtype);
	    if (checksum != null && !checksum.equals(""))
		_queryParameterValues.put("checksum", checksum);
	    if (mimetype != null && !mimetype.equals(""))
		_queryParameterValues.put("mimeType", mimetype);
	    if (logmessage != null && !logmessage.equals(""))
		_queryParameterValues.put("logMessage", logmessage);

	    String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	    //retVal = _dsDispatcher.doPOST(null, null, _url, _headerParameterValues, null);
	    //AMG mods
	    if (dslocation.startsWith("http")) {
		retVal = _dsDispatcher.doPOST(new ByteArrayDataSource("", mimetype), mimetype, _url, _headerParameterValues, null);
	    }else{
		retVal = _dsDispatcher.doPOST(new ByteArrayDataSource(dslocation, mimetype), mimetype, _url, _headerParameterValues, null);
	    }
	    return ;
	}

	public void post(UserCredentials userCreds, String controlgroup, String dslocation, String altids, String dslabel, Boolean versionable, String dsstate, String formaturi, String checksumtype, String checksum, String mimetype, String logmessage)
	throws IOException, MalformedURLException
	{
	    HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	    HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	    //AMG - Add credentials
	    _headerParameterValues.put("Authorization", userCreds.getEncodedCredentials());
	    if (dslocation.startsWith("http")) {
		_queryParameterValues.put("dsLocation", dslocation);
	    }
	    if (controlgroup != null && !controlgroup.equals(""))
		_queryParameterValues.put("controlGroup", controlgroup);
	    if (altids != null && !altids.equals(""))
		_queryParameterValues.put("altIDs", altids);
	    if (dslabel != null && !dslabel.equals(""))
		_queryParameterValues.put("dsLabel", dslabel);
	    if (versionable != null && !versionable.equals(""))
		_queryParameterValues.put("versionable", versionable);
	    if (dsstate != null && !dsstate.equals(""))
		_queryParameterValues.put("dsState", dsstate);
	    if (formaturi != null && !formaturi.equals(""))
		_queryParameterValues.put("formatURI", formaturi);
	    if (checksumtype != null && !checksumtype.equals(""))
		_queryParameterValues.put("checksumType", checksumtype);
	    if (checksum != null && !checksum.equals(""))
		_queryParameterValues.put("checksum", checksum);
	    if (mimetype != null && !mimetype.equals(""))
		_queryParameterValues.put("mimeType", mimetype);
	    if (logmessage != null && !logmessage.equals(""))
		_queryParameterValues.put("logMessage", logmessage);
	    String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	    //retVal = _dsDispatcher.doPOST(null, null, _url, _headerParameterValues, null);
	    //AMG mods
	    if (dslocation.startsWith("http")) {
		retVal = _dsDispatcher.doPOST(new ByteArrayDataSource("", mimetype), mimetype, _url, _headerParameterValues, null);
	    }else{
		retVal = _dsDispatcher.doPOST(new ByteArrayDataSource(dslocation, mimetype), mimetype, _url, _headerParameterValues, null);
	    }
	    return ;
	}

	/*public void postAsvoid(String controlgroup, String dslocation, String altids, String dslabel, Boolean versionable, String dsstate, String formaturi, String checksumtype, String checksum, String mimetype, String logmessage)
	throws IOException, MalformedURLException, JAXBException
	{
	    HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	    HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	    _queryParameterValues.put("controlGroup", controlgroup);
	    _queryParameterValues.put("dsLocation", dslocation);
	    _queryParameterValues.put("altIDs", altids);
	    _queryParameterValues.put("dsLabel", dslabel);
	    _queryParameterValues.put("versionable", versionable);
	    _queryParameterValues.put("dsState", dsstate);
	    _queryParameterValues.put("formatURI", formaturi);
	    _queryParameterValues.put("checksumType", checksumtype);
	    _queryParameterValues.put("checksum", checksum);
	    _queryParameterValues.put("mimeType", mimetype);
	    _queryParameterValues.put("logMessage", logmessage);
	    String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	    Object _retVal = _jaxbDispatcher.doPOST(null, null, _url, _headerParameterValues, null);
	    if (_retVal == null) {
		return ;
	    }
	    if (JAXBElement.class.isInstance(_retVal)) {
		JAXBElement<?> jaxbElement = ((JAXBElement<?>) _retVal);
		_retVal = jaxbElement.getValue();
	    }
	    return ;
	}*/

	public void put()
	throws IOException, MalformedURLException
	{
	    HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	    HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	    String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	    retVal = _dsDispatcher.doPUT(null, null, _url, _headerParameterValues, null);
	    return ;
	}

	public void put(UserCredentials userCreds)
	throws IOException, MalformedURLException
	{
	    HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	    HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	    //AMG - Add credentials
	    _headerParameterValues.put("Authorization", userCreds.getEncodedCredentials());
	    String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	    retVal = _dsDispatcher.doPUT(null, null, _url, _headerParameterValues, null);
	    return ;
	}

	public void putAsvoid()
	throws IOException, MalformedURLException, JAXBException
	{
	    HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	    HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	    String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	    Object _retVal = _jaxbDispatcher.doPUT(null, null, _url, _headerParameterValues, null);
	    if (_retVal == null) {
		return ;
	    }
	    if (JAXBElement.class.isInstance(_retVal)) {
		JAXBElement<?> jaxbElement = ((JAXBElement<?>) _retVal);
		_retVal = jaxbElement.getValue();
	    }
	    return ;
	}

	public void put(String dslocation, String altids, String dslabel, Boolean versionable, String dsstate, String formaturi, String checksumtype, String checksum, String mimetype, String logmessage, Boolean ignorecontent, String lastmodifieddate)
	throws IOException, MalformedURLException
	{
	    HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	    HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	    if (dslocation.startsWith("http"))
		_queryParameterValues.put("dsLocation", dslocation);
	    if (altids != null && !altids.equals(""))
		_queryParameterValues.put("altIDs", altids);
	    if (dslabel != null && !dslabel.equals(""))
		_queryParameterValues.put("dsLabel", dslabel);
	    if (versionable != null && !versionable.equals(""))
		_queryParameterValues.put("versionable", versionable);
	    if (dsstate != null && !dsstate.equals(""))
		_queryParameterValues.put("dsState", dsstate);
	    if (formaturi != null && !formaturi.equals(""))
		_queryParameterValues.put("formatURI", formaturi);
	    if (checksumtype != null && !checksumtype.equals(""))
		_queryParameterValues.put("checksumType", checksumtype);
	    if (checksum != null && !checksum.equals(""))
		_queryParameterValues.put("checksum", checksum);
	    if (mimetype != null && !mimetype.equals(""))
		_queryParameterValues.put("mimeType", mimetype);
	    if (logmessage != null && !logmessage.equals(""))
		_queryParameterValues.put("logMessage", logmessage);
	    if (ignorecontent != null && !ignorecontent.equals("")) {
		if (dslocation.startsWith("http"))
		    _queryParameterValues.put("ignoreContent", true);
		else _queryParameterValues.put("ignoreContent", ignorecontent);
	    }	
	    if (lastmodifieddate != null && !lastmodifieddate.equals(""))
		_queryParameterValues.put("lastModifiedDate", lastmodifieddate);
	    String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	    
	    if (dslocation.startsWith("http")) {
		retVal = _dsDispatcher.doPUT(new ByteArrayDataSource("", mimetype), mimetype, _url, _headerParameterValues, null);
	    }else{
		retVal = _dsDispatcher.doPUT(new ByteArrayDataSource(dslocation, mimetype), mimetype, _url, _headerParameterValues, null);
	    }
	    return ;
	}

	public void put(UserCredentials userCreds, String dslocation, String altids, String dslabel, Boolean versionable, String dsstate, String formaturi, String checksumtype, String checksum, String mimetype, String logmessage, Boolean ignorecontent, String lastmodifieddate)
	throws IOException, MalformedURLException
	{
	    HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	    HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	    //AMG - Add credentials
	    _headerParameterValues.put("Authorization", userCreds.getEncodedCredentials());
	    if (dslocation.startsWith("http"))
		_queryParameterValues.put("dsLocation", dslocation);
	    if (altids != null && !altids.equals(""))
		_queryParameterValues.put("altIDs", altids);
	    if (dslabel != null && !dslabel.equals(""))
		_queryParameterValues.put("dsLabel", dslabel);
	    if (versionable != null && !versionable.equals(""))
		_queryParameterValues.put("versionable", versionable);
	    if (dsstate != null && !dsstate.equals(""))
		_queryParameterValues.put("dsState", dsstate);
	    if (formaturi != null && !formaturi.equals(""))
		_queryParameterValues.put("formatURI", formaturi);
	    if (checksumtype != null && !checksumtype.equals(""))
		_queryParameterValues.put("checksumType", checksumtype);
	    if (checksum != null && !checksum.equals(""))
		_queryParameterValues.put("checksum", checksum);
	    if (mimetype != null && !mimetype.equals(""))
		_queryParameterValues.put("mimeType", mimetype);
	    if (logmessage != null && !logmessage.equals(""))
		_queryParameterValues.put("logMessage", logmessage);
	    if (ignorecontent != null && !ignorecontent.equals("")) {
		if (dslocation.startsWith("http"))
		    _queryParameterValues.put("ignoreContent", true);
		else _queryParameterValues.put("ignoreContent", ignorecontent);
	    }		
	    if (lastmodifieddate != null && !lastmodifieddate.equals(""))
		_queryParameterValues.put("lastModifiedDate", lastmodifieddate);

	    String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	    
	    if (dslocation.startsWith("http")) {
		retVal = _dsDispatcher.doPUT(new ByteArrayDataSource("", mimetype), mimetype, _url, _headerParameterValues, null);
	    }else{
		retVal = _dsDispatcher.doPUT(new ByteArrayDataSource(dslocation, mimetype), mimetype, _url, _headerParameterValues, null);
	    }
	    return ;
	}

	/*public void putAsvoid(UserCredentials userCreds, String dslocation, String altids, String dslabel, Boolean versionable, String dsstate, String formaturi, String checksumtype, String checksum, String mimetype, String logmessage, Boolean ignorecontent, String lastmodifieddate)
	throws IOException, MalformedURLException, JAXBException
	{
	    HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	    HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	  //AMG - Add credentials
	    _headerParameterValues.put("Authorization", userCreds.getEncodedCredentials());
	    _queryParameterValues.put("dsLocation", dslocation);
	    _queryParameterValues.put("altIDs", altids);
	    _queryParameterValues.put("dsLabel", dslabel);
	    _queryParameterValues.put("versionable", versionable);
	    _queryParameterValues.put("dsState", dsstate);
	    _queryParameterValues.put("formatURI", formaturi);
	    _queryParameterValues.put("checksumType", checksumtype);
	    _queryParameterValues.put("checksum", checksum);
	    _queryParameterValues.put("mimeType", mimetype);
	    _queryParameterValues.put("logMessage", logmessage);
	    _queryParameterValues.put("ignoreContent", ignorecontent);
	    _queryParameterValues.put("lastModifiedDate", lastmodifieddate);
	    String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	    Object _retVal = _jaxbDispatcher.doPUT(null, null, _url, _headerParameterValues, null);
	    if (_retVal == null) {
		return ;
	    }
	    if (JAXBElement.class.isInstance(_retVal)) {
		JAXBElement<?> jaxbElement = ((JAXBElement<?>) _retVal);
		_retVal = jaxbElement.getValue();
	    }
	    return ;
	}*/

	public void delete()
	throws IOException, MalformedURLException
	{
	    HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	    HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	    String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	    retVal = _dsDispatcher.doDELETE(_url, _headerParameterValues, null);
	    return ;
	}

	public void delete(UserCredentials userCreds)
	throws IOException, MalformedURLException
	{
	    HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	    HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	    //AMG - Add credentials
	    _headerParameterValues.put("Authorization", userCreds.getEncodedCredentials());
	    String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	    retVal = _dsDispatcher.doDELETE(_url, _headerParameterValues, null);
	    return ;
	}

	/*public void deleteAsvoid()
	throws IOException, MalformedURLException, JAXBException
	{
	    HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	    HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	    String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	    Object _retVal = _jaxbDispatcher.doDELETE(_url, _headerParameterValues, null);
	    if (_retVal == null) {
		return ;
	    }
	    if (JAXBElement.class.isInstance(_retVal)) {
		JAXBElement<?> jaxbElement = ((JAXBElement<?>) _retVal);
		_retVal = jaxbElement.getValue();
	    }
	    return ;
	}*/

	public void delete(String startdt, String enddt, String logmessage)
	throws IOException, MalformedURLException
	{
	    HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	    HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	    if (startdt != null && !startdt.equals(""))
		_queryParameterValues.put("startDT", startdt);
	    if (enddt != null && !enddt.equals(""))
		_queryParameterValues.put("endDT", enddt);
	    if (logmessage != null && !logmessage.equals(""))
		_queryParameterValues.put("logMessage", logmessage);
	    String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	    retVal = _dsDispatcher.doDELETE(_url, _headerParameterValues, null);
	    return ;
	}

	public void delete(UserCredentials userCreds, String startdt, String enddt, String logmessage)
	throws IOException, MalformedURLException
	{
	    HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	    HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	    //AMG - Add credentials
	    _headerParameterValues.put("Authorization", userCreds.getEncodedCredentials());
	    if (startdt != null && !startdt.equals(""))
		_queryParameterValues.put("startDT", startdt);
	    if (enddt != null && !enddt.equals(""))
		_queryParameterValues.put("endDT", enddt);
	    if (logmessage != null && !logmessage.equals(""))
		_queryParameterValues.put("logMessage", logmessage);
	    String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	    retVal = _dsDispatcher.doDELETE(_url, _headerParameterValues, null);
	    return ;
	}

	/*public void deleteAsvoid(String startdt, String enddt, String logmessage)
	throws IOException, MalformedURLException, JAXBException
	{
	    HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	    HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	    _queryParameterValues.put("startDT", startdt);
	    _queryParameterValues.put("endDT", enddt);
	    _queryParameterValues.put("logMessage", logmessage);
	    String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	    Object _retVal = _jaxbDispatcher.doDELETE(_url, _headerParameterValues, null);
	    if (_retVal == null) {
		return ;
	    }
	    if (JAXBElement.class.isInstance(_retVal)) {
		JAXBElement<?> jaxbElement = ((JAXBElement<?>) _retVal);
		_retVal = jaxbElement.getValue();
	    }
	    return ;
	}*/

	public void get()
	throws IOException, MalformedURLException
	{
	    HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	    HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	    String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	    retVal = _dsDispatcher.doGET(_url, _headerParameterValues, null);
	    return ;
	}

	public void get(UserCredentials userCreds)
	throws IOException, MalformedURLException
	{
	    HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	    HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	    //AMG - Add credentials
	    _headerParameterValues.put("Authorization", userCreds.getEncodedCredentials());
	    String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	    retVal = _dsDispatcher.doGET(_url, _headerParameterValues, null);
	    return ;
	}

	/*public void getAsvoid()
	throws IOException, MalformedURLException, JAXBException
	{
	    HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	    HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	    String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	    Object _retVal = _jaxbDispatcher.doGET(_url, _headerParameterValues, null);
	    if (_retVal == null) {
		return ;
	    }
	    if (JAXBElement.class.isInstance(_retVal)) {
		JAXBElement<?> jaxbElement = ((JAXBElement<?>) _retVal);
		_retVal = jaxbElement.getValue();
	    }
	    return ;
	}*/

	public void get(String asofdatetime, String format, Boolean validatechecksum)
	throws IOException, MalformedURLException
	{
	    HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	    HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	    if (asofdatetime != null && !asofdatetime.equals(""))
		_queryParameterValues.put("asOfDateTime", asofdatetime);
	    if (format != null && !format.equals(""))
		_queryParameterValues.put("format", format);
	    if (validatechecksum != null && !validatechecksum.equals(""))
		_queryParameterValues.put("validateChecksum", validatechecksum);
	    String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	    retVal = _dsDispatcher.doGET(_url, _headerParameterValues, null);
	    return ;
	}

	public void get(UserCredentials userCreds, String asofdatetime, String format, Boolean validatechecksum)
	throws IOException, MalformedURLException
	{
	    HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	    HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	    //AMG - Add credentials
	    _headerParameterValues.put("Authorization", userCreds.getEncodedCredentials());
	    if (asofdatetime != null && !asofdatetime.equals(""))
		_queryParameterValues.put("asOfDateTime", asofdatetime);
	    if (format != null && !format.equals(""))
		_queryParameterValues.put("format", format);
	    if (validatechecksum != null && !validatechecksum.equals(""))
		_queryParameterValues.put("validateChecksum", validatechecksum);
	    String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	    retVal = _dsDispatcher.doGET(_url, _headerParameterValues, null);
	    return ;
	}

	/*public void getAsvoid(String asofdatetime, String format, Boolean validatechecksum)
	throws IOException, MalformedURLException, JAXBException
	{
	    HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	    HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	    _queryParameterValues.put("asOfDateTime", asofdatetime);
	    _queryParameterValues.put("format", format);
	    _queryParameterValues.put("validateChecksum", validatechecksum);
	    String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	    Object _retVal = _jaxbDispatcher.doGET(_url, _headerParameterValues, null);
	    if (_retVal == null) {
		return ;
	    }
	    if (JAXBElement.class.isInstance(_retVal)) {
		JAXBElement<?> jaxbElement = ((JAXBElement<?>) _retVal);
		_retVal = jaxbElement.getValue();
	    }
	    return ;
	}*/

    }

    public static class DsIDContent {

	//private JAXBDispatcher _jaxbDispatcher;
	private DSDispatcher _dsDispatcher;
	private UriBuilder _uriBuilder;
	@SuppressWarnings("unused")
	private JAXBContext _jc;
	private HashMap<String, Object> _templateAndMatrixParameterValues;
	private List<String> matrixParamSet;
	private DataSource retVal;

	public void setMatrixParamSet(List<String> matrixParamSet) {
	    this.matrixParamSet = matrixParamSet;
	}

	public List<String> getMatrixParamSet() {
	    return matrixParamSet;
	}

	public void setRetVal(DataSource retVal) {
	    this.retVal = retVal;
	}

	public DataSource getRetVal() {
	    return retVal;
	}

	/**
	 * Create new instance
	 * 
	 */
	public DsIDContent(String pid, String dsid)
	throws JAXBException
	{
	    _dsDispatcher = new DSDispatcher();
	    _uriBuilder = new UriBuilder();
	    String basePath;
	    try {
		basePath = JFig.getInstance().getValue("fedoraServer", "restService");
	    } catch (JFigException e) {
		log.error("Error: ", e);
		//Error, use default Fedora path
		basePath = "http://localhost:8080/fedora";
	    }
	    matrixParamSet = _uriBuilder.addPathSegment(basePath);
	    matrixParamSet = _uriBuilder.addPathSegment("/objects");
	    matrixParamSet = _uriBuilder.addPathSegment("/{pid}/datastreams");
	    matrixParamSet = _uriBuilder.addPathSegment("/{dsID}/content");
	    _templateAndMatrixParameterValues = new HashMap<String, Object>();
	    _templateAndMatrixParameterValues.put("pid", pid);
	    _templateAndMatrixParameterValues.put("dsID", dsid);
	}

	/**
	 * Get pid
	 * 
	 */
	public String getPid() {
	    return ((String) _templateAndMatrixParameterValues.get("pid"));
	}

	/**
	 * Set pid
	 * 
	 */
	public void setPid(String pid) {
	    _templateAndMatrixParameterValues.put("pid", pid);
	}

	/**
	 * Get dsID
	 * 
	 */
	public String getDsid() {
	    return ((String) _templateAndMatrixParameterValues.get("dsID"));
	}

	/**
	 * Set dsID
	 * 
	 */
	public void setDsid(String dsid) {
	    _templateAndMatrixParameterValues.put("dsID", dsid);
	}

	public void get()
	throws IOException, MalformedURLException
	{
	    HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	    HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	    String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	    retVal = _dsDispatcher.doGET(_url, _headerParameterValues, null);
	    return ;
	}

	public void get(UserCredentials userCreds)
	throws IOException, MalformedURLException
	{
	    HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	    HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	    //AMG - Add credentials
	    _headerParameterValues.put("Authorization", userCreds.getEncodedCredentials());
	    String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	    retVal = _dsDispatcher.doGET(_url, _headerParameterValues, null);
	    return ;
	}

	/*public void getAsvoid()
	throws IOException, MalformedURLException, JAXBException
	{
	    HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	    HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	    String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	    Object _retVal = _jaxbDispatcher.doGET(_url, _headerParameterValues, null);
	    if (_retVal == null) {
		return ;
	    }
	    if (JAXBElement.class.isInstance(_retVal)) {
		JAXBElement<?> jaxbElement = ((JAXBElement<?>) _retVal);
		_retVal = jaxbElement.getValue();
	    }
	    return ;
	}*/

	public void get(String asofdatetime, String download)
	throws IOException, MalformedURLException
	{
	    HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	    HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	    if (asofdatetime != null && !asofdatetime.equals(""))
		_queryParameterValues.put("asOfDateTime", asofdatetime);
	    if (download != null && !download.equals(""))
		_queryParameterValues.put("download", download);
	    String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	    retVal = _dsDispatcher.doGET(_url, _headerParameterValues, null);
	    return ;
	}

	public void get(UserCredentials userCreds, String asofdatetime, String download)
	throws IOException, MalformedURLException
	{
	    HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	    HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	    //AMG - Add credentials
	    _headerParameterValues.put("Authorization", userCreds.getEncodedCredentials());
	    if (asofdatetime != null && !asofdatetime.equals(""))
		_queryParameterValues.put("asOfDateTime", asofdatetime);
	    if (download != null && !download.equals(""))
		_queryParameterValues.put("download", download);
	    String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	    retVal = _dsDispatcher.doGET(_url, _headerParameterValues, null);
	    return ;
	}

	/*public void getAsvoid(String asofdatetime, String download)
	throws IOException, MalformedURLException, JAXBException
	{
	    HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	    HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	    _queryParameterValues.put("asOfDateTime", asofdatetime);
	    _queryParameterValues.put("download", download);
	    String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	    Object _retVal = _jaxbDispatcher.doGET(_url, _headerParameterValues, null);
	    if (_retVal == null) {
		return ;
	    }
	    if (JAXBElement.class.isInstance(_retVal)) {
		JAXBElement<?> jaxbElement = ((JAXBElement<?>) _retVal);
		_retVal = jaxbElement.getValue();
	    }
	    return ;
	}*/

    }

    public static class DsIDHistory {

	//private JAXBDispatcher _jaxbDispatcher;
	private DSDispatcher _dsDispatcher;
	private UriBuilder _uriBuilder;
	@SuppressWarnings("unused")
	private JAXBContext _jc;
	private HashMap<String, Object> _templateAndMatrixParameterValues;
	private List<String> matrixParamSet;
	private DataSource retVal;

	public void setMatrixParamSet(List<String> matrixParamSet) {
	    this.matrixParamSet = matrixParamSet;
	}

	public List<String> getMatrixParamSet() {
	    return matrixParamSet;
	}

	public void setRetVal(DataSource retVal) {
	    this.retVal = retVal;
	}

	public DataSource getRetVal() {
	    return retVal;
	}

	/**
	 * Create new instance
	 * 
	 */
	public DsIDHistory(String pid, String dsid)
	throws JAXBException
	{
	    _dsDispatcher = new DSDispatcher();
	    _uriBuilder = new UriBuilder();
	    String basePath;
	    try {
		basePath = JFig.getInstance().getValue("fedoraServer", "restService");
	    } catch (JFigException e) {
		log.error("Error: ", e);
		//Error, use default Fedora path
		basePath = "http://localhost:8080/fedora";
	    }
	    matrixParamSet = _uriBuilder.addPathSegment(basePath);
	    matrixParamSet = _uriBuilder.addPathSegment("/objects");
	    matrixParamSet = _uriBuilder.addPathSegment("/{pid}/datastreams");
	    matrixParamSet = _uriBuilder.addPathSegment("/{dsID}/history");
	    _templateAndMatrixParameterValues = new HashMap<String, Object>();
	    _templateAndMatrixParameterValues.put("pid", pid);
	    _templateAndMatrixParameterValues.put("dsID", dsid);
	}

	/**
	 * Get pid
	 * 
	 */
	public String getPid() {
	    return ((String) _templateAndMatrixParameterValues.get("pid"));
	}

	/**
	 * Set pid
	 * 
	 */
	public void setPid(String pid) {
	    _templateAndMatrixParameterValues.put("pid", pid);
	}

	/**
	 * Get dsID
	 * 
	 */
	public String getDsid() {
	    return ((String) _templateAndMatrixParameterValues.get("dsID"));
	}

	/**
	 * Set dsID
	 * 
	 */
	public void setDsid(String dsid) {
	    _templateAndMatrixParameterValues.put("dsID", dsid);
	}

	public void get()
	throws IOException, MalformedURLException
	{
	    HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	    HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	    String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	    retVal = _dsDispatcher.doGET(_url, _headerParameterValues, null);
	    return ;
	}

	public void get(UserCredentials userCreds)
	throws IOException, MalformedURLException
	{
	    HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	    HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	    //AMG - Add credentials
	    _headerParameterValues.put("Authorization", userCreds.getEncodedCredentials());
	    String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	    retVal = _dsDispatcher.doGET(_url, _headerParameterValues, null);
	    return ;
	}

	/*public void getAsvoid()
	throws IOException, MalformedURLException, JAXBException
	{
	    HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	    HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	    String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	    Object _retVal = _jaxbDispatcher.doGET(_url, _headerParameterValues, null);
	    if (_retVal == null) {
		return ;
	    }
	    if (JAXBElement.class.isInstance(_retVal)) {
		JAXBElement<?> jaxbElement = ((JAXBElement<?>) _retVal);
		_retVal = jaxbElement.getValue();
	    }
	    return ;
	}*/

	public void get(String format)
	throws IOException, MalformedURLException
	{
	    HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	    HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	    if (format != null && !format.equals(""))
		_queryParameterValues.put("format", format);
	    String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	    retVal = _dsDispatcher.doGET(_url, _headerParameterValues, null);
	    return ;
	}

	public void get(UserCredentials userCreds, String format)
	throws IOException, MalformedURLException
	{
	    HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	    HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	    //AMG - Add credentials
	    _headerParameterValues.put("Authorization", userCreds.getEncodedCredentials());
	    if (format != null && !format.equals(""))
		_queryParameterValues.put("format", format);
	    String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	    retVal = _dsDispatcher.doGET(_url, _headerParameterValues, null);
	    return ;
	}

	/*public void getAsvoid(String format)
	throws IOException, MalformedURLException, JAXBException
	{
	    HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	    HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	    _queryParameterValues.put("format", format);
	    String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	    Object _retVal = _jaxbDispatcher.doGET(_url, _headerParameterValues, null);
	    if (_retVal == null) {
		return ;
	    }
	    if (JAXBElement.class.isInstance(_retVal)) {
		JAXBElement<?> jaxbElement = ((JAXBElement<?>) _retVal);
		_retVal = jaxbElement.getValue();
	    }
	    return ;
	}*/

    }

}